package org.authsum.stitches.wicket.attribute;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.StringResourceModel;
import org.authsum.stitches.external.*;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.authsum.stitches.wicket.*;
import org.authsum.stitches.wicket.contentdef.*;


import java.util.*;



public class AttributesPanel extends BasePanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private static Log log = LogFactory.getLog(AttributesPanel.class);

	 ContentDefHolder contentDefHolder = null;

	 ContentDefsPanel contentDefsPanel = null;
	public AttributesPanel(String id, ContentDefHolder contentDefHolder,ContentDefsPanel contentDefsPanel) {
		super(id);

		this.contentDefsPanel = contentDefsPanel;
		this.contentDefHolder = contentDefHolder;
		//this.setOutputMarkupId(true);


		List<AttributeHolder> attributeHolders =  new ArrayList<AttributeHolder>();
		
		// List<Site> sitelist = new ArrayList<Site> ();
		// sitelist.addAll(allSites);
		
		if ( contentDefHolder.getAttributeHolders()  != null) {
			attributeHolders =contentDefHolder.getAttributeHolders().getAttributeHolder();
		}
		
		ListView attributesListView = new ListView("attributesListView", attributeHolders) {
			protected void populateItem(ListItem item) {

				item.add(new AttributePanel(item,"name"));
				//item.add(arg0)
			}
		};

		addOrReplace(attributesListView);

	}
	
	

	public ContentDefsPanel getContentDefsPanel() {
		return contentDefsPanel;
	}
	
	private AjaxLink createDeleteLink(final ListItem listItem) {

		
		AjaxLink link = (new AjaxLink("removeAttributeLink", listItem.getModel()) {

			@Override
			public void onClick(AjaxRequestTarget target) {
					AttributeHolder attributeHolder = (AttributeHolder) getModel().getObject();
				//target.addComponent(dmiPanel);
					ContentDefRequest contentDefRequest = new ContentDefRequest();
					contentDefRequest.setContentDefId(contentDefHolder.getId());
					contentDefRequest.setAttributeHolder(attributeHolder);

					
					
					ContentDefResponse cdr = getStitchesService().removeAttribute(contentDefRequest);
					contentDefHolder.getAttributeHolders().getAttributeHolder().remove(attributeHolder);
					
					getContentDefsPanel().editContentDef(target,contentDefHolder);
					
					//target.addComponent(attributesPanel);
					
			}

		});
		
		return link;

	}
	
	
	
	private class AttributePanel extends BasePanel {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;

		public AttributePanel(ListItem listItem, String id) {
			super(id);
			AjaxLink link = createDeleteLink(listItem);
			add(link);
			AttributeHolder attributeHolder = (AttributeHolder) listItem.getModel().getObject();
			String attributeNameLabel = attributeHolder.getName();
			add(new Label("attributeNameLabel", attributeNameLabel));
			
		}

	}
	
	


}
